package com.kvn.mockj;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import java.util.*;

public class JsonUtils {

    /**
     * 将json对象转换为HashMap
     * @param json
     * @return
     */


    public static Map<String, Object> parseJSON2Map(JSONObject json) {
        Map<String, Object> map = new HashMap<String, Object>();
        // 最外层解析
        for (Object k : json.keySet()) {
            Object v = json.get(k);
            // 如果内层还是json数组的话，继续解析
            if (v instanceof JSONArray) {
                List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
                Iterator<JSONObject> it = ((JSONArray) v).iterator();
                while (it.hasNext()) {
                    JSONObject json2 = it.next();
                    list.add(parseJSON2Map(json2));
                }
                map.put(k.toString(), list);
            } else if (v instanceof JSONObject) {
                // 如果内层是json对象的话，继续解析
                map.put(k.toString(), parseJSON2Map((JSONObject) v));
            } else {
                // 如果内层是普通对象的话，直接放入map中
                map.put(k.toString(), v);
            }
        }
        return map;
    }

    /**
     * 将json字符串转换为Map
     * @param jsonStr
     * @return
     */
    public static Map<String, Object> parseJSONstr2Map(String jsonStr) {
        JSONObject json = JSONObject.fromObject(jsonStr);
        Map<String, Object> map = parseJSON2Map(json);
        return map;
    }
    public static void main(String[] args) {
        Map<String, Object> myresult = JsonUtils.parseJSONstr2Map("{" +
                "\"data\": {" +
                "\"studentList\": [{" +
                "\"scourse\": {" +
                "\"cname\": \"语文,数学,英语\"," +
                "\"cscore\": \"93,95,98\"" +
                "}," +
                "\"sname\": \"张三\"," +
                "\"sage\": \"20\"," +
                "\"sid\": \"101\"" +
                "}," +
                "{" +
                "\"scourse\": {" +
                "\"cname\": \"物理,化学,生物\"," +
                "\"cscore\": \"92,93,97\"" +
                "}," +
                "\"sname\": \"李四\"," +
                "\"sage\": \"30\"," +
                "\"sid\": \"102\"" +
                "}]" +
                "}," +
                "\"resultCode\": \"1\"" +
                "}");

        System.out.println(myresult);
    }
    public static String  getString() {
        Map<String, Object> mock = new HashMap<String, Object>(){{
            put("name", "@str(1-3):\"张三\"");
            put("order", "xx");
            put("sage", "@num(1-20):20");
        }};

       return JSONObject.fromObject(mock).toString();
    }

}

